package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
//        Appointment oldObj = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {//判断状态
            throw new BusinessException("只有预约中才能编辑");
        }
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);//更改它的状态
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            Appointment appointment = this.get(dictId);
            if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
                throw new BusinessException("只有预约中才能删除");
            }
            appointmentMapper.deleteByPrimaryKey(dictId, appointment.getDeleteFlag());
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }


    @Override
    public void cancel(Long id, Appointment appointment) {//取消
        Appointment oldObj = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("只有预约中才能取消");
        }
        appointmentMapper.changeStatus(id, Appointment.STATUS_CANCEL);
    }

    @Override
    public void arrival(Long id, Appointment appointment) {//到店
        Appointment oldObj = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("只有处于预约中才能到店");
        }
        appointmentMapper.arrivalTime(id, new Date());
        appointmentMapper.changeStatus(id, Appointment.STATUS_ARRIVAL);
    }

    @Override
    @Transactional
    public Long generateStatement(Long appointmentId) {
        //合理性判断
        Appointment appointment = this.get(appointmentId);
        if (!(Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())
                || Appointment.STATUS_SETTLE.equals(appointment.getStatus()))
        ) {
            throw new BusinessException("只有处于已到店和已结算状态才能进行结算单处理");
        }
        //根据预约单ID查询对应的结算单
        Statement statement = statementService.getByAppointmentId(appointmentId);
        if (statement == null) {
            //没记录,说明此预约单没有生成结算单记录
            statement=new Statement();
            statement.setCreateTime(new Date());//创建时间
            statement.setActualArrivalTime(appointment.getActualArrivalTime());//实际到店时间
            statement.setCarSeries(appointment.getCarSeries());//汽车类型
            statement.setCustomerName(appointment.getCustomerName());//客户名称
            statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
            statement.setAppointmentId(appointment.getId());//预约单ID
            statement.setServiceType(appointment.getServiceType().longValue());//服务类型
            statement.setCustomerPhone(appointment.getCustomerPhone());//用户联系方式
            statementService.save(statement);//保存结算单
        }
        appointmentMapper.changeStatus(appointmentId,Appointment.STATUS_SETTLE);//修改预约单状态
        return statement.getId();
    }
}
